import axios from "axios";
import {ElMessage} from "element-plus";

const _import = (component)=> () => import(`@/views/${component}.vue`);

//添加动态路由
export default async ()=>{
    try {
        const staffInfo = JSON.parse(localStorage.getItem('staffInfo'))
        const response = await axios.post('http://localhost:8888/rights/get_by_staff_id',{staffId:staffInfo.staffId})
        if (response.data.code === '00000'){
            const rootMenuNode = response.data.data||[]
            const menuItemList = rootMenuNode.childrenNode
            const dynamicRoute = []
            assemblyMenuNode(menuItemList, dynamicRoute)
            return dynamicRoute
        }else {
            ElMessage.error(response.data.message)
        }
    }catch (error) {
        console.log(error.message)
    }
}

/** 递归组装路由以及节点信息 **/
function assemblyMenuNode(menuNodeList,dynamicRoute){
    if (!menuNodeList) return
    menuNodeList.forEach(menuNode => {
        if (menuNode.childrenNode && menuNode.isLeaf == 'N'){
            assemblyMenuNode(menuNode.childrenNode, dynamicRoute)
        }else {
            /** 叶子节点 **/
            dynamicRoute.push({
                path:menuNode.routePath,
                name:menuNode.routeName,
                component:_import(menuNode.component)
            })
        }
    })
}